package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.QueryBusStandBykeyReq;
import sz.szsmk.citizencard.model.network.bean.request.QueryLineByLineNameReq;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryNearStandbyRes;
import sz.szsmk.citizencard.mvp.contract.IBusHistoryContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/21 16:27
 * description:
 */
public class BusHistoryPresenter extends IBasePresenter
        implements IBusHistoryContact.IBusHistoryPresenter<IBusHistoryContact.IBusHistoryView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public BusHistoryPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void queryBusStandByKey(String inputKey) {
        QueryBusStandBykeyReq queryBusStandBykeyReq = new QueryBusStandBykeyReq();
        queryBusStandBykeyReq.setStand_key_name(inputKey);
//        queryBusStandBykeyReq.setUserID(keyword);
        String json = new Gson().toJson(queryBusStandBykeyReq);
        mNetworkDateSource.queryBusStandByKey(queryBusStandBykeyReq, new SubscribeResult<QueryNearStandbyRes>() {
            @Override
            protected void onOk(QueryNearStandbyRes response) {
                String json1 = new Gson().toJson(response);
                ((IBusHistoryContact.IBusHistoryView) mView).queryBusStandByKeySucc(response);
            }

            @Override
            protected void onFailure(QueryNearStandbyRes response) {
                super.onFailure(response);
                ((IBusHistoryContact.IBusHistoryView) mView).queryBusStandByKeyFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    @Override
    public void queryLineByLineName(String inputKey) {
        QueryLineByLineNameReq byLineNameReq = new QueryLineByLineNameReq();
        byLineNameReq.setBus_line_name(inputKey);
        byLineNameReq.setUserID("6");
        String json = new Gson().toJson(byLineNameReq);
        mNetworkDateSource.queryLineByLineName(byLineNameReq, new SubscribeResult<QueryBusLineNearbyRes>() {
            @Override
            protected void onOk(QueryBusLineNearbyRes response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, json);
                ((IBusHistoryContact.IBusHistoryView) mView).queryLineByLineNameSucc(response);
            }

            @Override
            protected void onFailure(QueryBusLineNearbyRes response) {
                super.onFailure(response);
                ((IBusHistoryContact.IBusHistoryView) mView).queryLineByLineNameFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }
}
